﻿namespace ContactsSync.Providers.Outlook.ViewModels
{
    using System.Collections.Generic;
    using ContactsSync.Providers.ViewModels;
    using Infrastructure.Events;
    using Infrastructure.Models;
    using Microsoft.Practices.Composite.Events;
    using Microsoft.Practices.Composite.Presentation.Commands;
    using Models;
    using Providers;

    public class OutlookViewModel : BaseProviderViewModel
    {
        private readonly IOutlookContactsProvider outlookContactsProvider;

        private ContactFolder selectedContactFolder;

        public OutlookViewModel(IEventAggregator eventAggregator, IOutlookContactsProvider outlookContactsProvider)
            : base(eventAggregator)
        {
            this.outlookContactsProvider = outlookContactsProvider;

            this.ContactFolders = this.outlookContactsProvider.RetrieveContactFolders();
            this.selectedContactFolder = this.ContactFolders[0];

            this.SetProvider(outlookContactsProvider);

            this.UpdateContactCommand = new DelegateCommand<ContactInfo>(this.UpdateContact);

            eventAggregator.GetEvent<AddContactEvent>().Subscribe(this.AddContact, true);

            this.outlookContactsProvider.SaveContactCompleted += this.OutlookContactsProvider_SaveContactCompleted;
        }

        public DelegateCommand<ContactInfo> UpdateContactCommand { get; private set; }

        public IList<ContactFolder> ContactFolders { get; private set; }

        public ContactFolder SelectedContactFolder
        {
            get
            {
                return this.selectedContactFolder;
            }

            set
            {
                this.selectedContactFolder = value;
                this.OnPropertyChanged("SelectedContactFolder");
                this.RetrieveContacts();
            }
        }

        protected override void RetrieveContacts()
        {
            this.IsLoading = true;
            this.Contacts.Clear();
            this.outlookContactsProvider.RetrieveContactsAsync(this.SelectedContactFolder);
        }

        private void AddContact(Contact contact)
        {
            if (!this.Contacts.Contains(contact))
            {
                this.Contacts.Add(contact);
            }
        }

        private void UpdateContact(ContactInfo contactInfo)
        {
            if (contactInfo.Index < this.Contacts.Count)
            {
                Contact contact = this.Contacts[contactInfo.Index];

                contact.ImageSource = contactInfo.Contact.ImageSource;

                this.outlookContactsProvider.SaveContactAsync(contact);
            }
        }

        private void OutlookContactsProvider_SaveContactCompleted(object sender, DataEventArgs<bool> e)
        {
            if (e.Value)
            {
                // do something;
            }
            else
            {
                // do something;
            }
        }
    }
}
